// pages/index/component/products/conciseProduct.js
import {
  list
} from '../../../../api/product'
const util = require('../../../../utils/util')

let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tagIds:[],
    productType:false,
    concise:true,
    products:[],
    pageNum: 0,
    pageSize: 20,
  },
  lifetimes:{
    attached(){
      if(this.data.info.rows!=''||this.data.info.rows!=null){
        this.data.pageSize=this.data.info.rows
        this.setData({
          pageSize:this.data.pageSize
        })
      }
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
        this.setData({
          tagIds:this.data.tagIds
        })
      this.loadProduct()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
      loadProduct(){
        var _this = this
        _this.data.pageNum += 1
        console.log(this.data.pageNum)
        console.log(this.data.pageSize)

        list({
          mchId:app.globalData.mchId,
          pageSize:this.data.pageSize,
          tagIds:this.data.tagIds,
          pageNum: _this.data.pageNum,
        }).then(res=>{
          if (_this.data.pageNum == 1) {
            _this.data.products = []
            this.setData({
              products: _this.data.products 
            })
          }
          res.data.data.forEach(item=>{
            item.thumbnail = util.thumbnail(item.thumbnail, 340, 360)
            this.data.products.push(item)
          })
          if (res.data.data.length < 20) {
            _this.data.productType = true
          }
          this.setData({
            productType:this.data.productType,
            products:this.data.products
          })
        })
      },
       //+++++++++++上拉加载+++++++++
    onReachBottom: function (callback) {
      this.loadProduct(() => {
        if (callback) {
          callback()
        }
      })

    },
    downRefresh: function () {
      this.data.products = []
      this.data.pageNum = 0
      this.loadProduct()
      this.setData({
        products: this.data.products,
        pageNum: this.data.pageNum
      })
    },
  }
})
